﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace com.hyrentals.common
{
    public class EncodeLib
    {
        public static string MD5String(string input)
        {
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
            return Convert.ToBase64String(s);
        }

        public static string BinaryStringToString(string input)
        {
            StringBuilder sb = new StringBuilder();
            byte[] barray = new byte[input.Length / 2];
            for (int i = 0; i < barray.Length; ++i)
            {
                barray[i] = Convert.ToByte(input.Substring(2 * i, 2), 16);
            }
            return Encoding.UTF8.GetString(barray);
        }

        public static string StringToBinaryString(string input)
        {
            StringBuilder sb = new StringBuilder();
            byte[] s = Encoding.UTF8.GetBytes(input);
            foreach (byte b in s)
            {
                sb.Append(b.ToString("X"));
            }

            return sb.ToString();
        }
    }
}
